package com.xy.netty.dome.echo.original;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
/**
 * @author 1
 */
@Slf4j
public class RefuseHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
        Socket socket = BioServer.getCurrentSocket().get();
        if(socket==null){
            return;
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(socket.getOutputStream(),true);
            out.println("The network is busy, please try again later");
        } catch (Exception e) {
            log.error("拒绝错误",e);
        }finally {
            if(out!=null){
                out.close();
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            BioServer.getCurrentSocket().remove();
        }
    }
}
